// pages/Order/OrderAddress/Orderdetails/Orderdetails.js
var order_id 
var url = wx.getStorageSync('url') 
var shopid = wx.getStorageSync('shopid') 
var userId = wx.getStorageSync('userId')
var openid = wx.getStorageSync('openid')
var time_ss = wx.getStorageSync('time_ss')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    condition1: false,
    condition2:false,
    condition3: true,
    condition4: false,

  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    order_id = options.order_id;
    console.log(options)
    var that = this;
    that.goto_request()
  },
  goto_payment:function(){
      wx.navigateTo({
        url: '../../payment/payment?order_id=' + order_id,
      })
  },
  goto_logistics:function(){
      wx.navigateTo({
        url: '../../logistics/logistics?order_id=' + order_id,
      })
  },
  goto_index:function(){
    wx.switchTab({
      url:'../../../index/index'
    })
  },
  goto_request: function (e) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    var that = this
    console.log(order_id)
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: url+'gateOrder/searchOrderById',
      data: {
        "list": false,
        "nodeId": "kdslfw",
        "platform": 2,
        "privId": "default",
        "resId": "default",
        "sendTime": time_ss,
        "seq": 1,
        "shopId": shopid,
        "source": 11,
        "sysVer": "1.0.0",
        "token": "kdsjfoewi12ijsldks",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "id": order_id
        }
      },
      method: "POST",
      header: { "Content-Type": "application/json" },
      success: function (e) {
        wx.hideLoading()
        var datas = e.data.content
        console.log(datas)
        that.setData({
          datas: datas
        })
      }
    })
  },
  // 更改状态-发货-付款-收货
  goto_Delivergoods: function (e) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    var id = e.currentTarget.id
    var status = e.currentTarget.dataset.status + 1
    var balance = e.currentTarget.dataset.balance
    var that = this
    // wx.showLoading({
    //   title: '加载中',
    // })
    var i = e.currentTarget.dataset.status
    if (i == 2) {
      // 发起支付
      wx.request({
        url: url +'gateCash/payByOrder',
        data: {
          "list": false,
          "nodeId": "kdslfw",
          "platform": 2,
          "privId": "default",
          "resId": "default",
          "sendTime": time_ss,
          "seq": 1,
          "shopId": shopid,
          "source": 11,
          "sysVer": "1.0.0",
          "token": "kdsjfoewi12ijsldks",
          "type": 1,
          "userId": userId,
          "userType": 1,
          "content": {
            "shopId": shopid,
            "openId": openid,
            "appId": "wx6ca4bda138687ed8",
            "orderId": id
          }
        },
        method: "POST",
        header: { "Content-Type": "application/json" },
        success: function (e) {
          var s = e.data.result.code.substring(4)
          var i_ = e.data.result.desc
          console.log(s)
          if (s == '9999') {  //余额不足调用微信接口
            console.log(e.data.content)
            console.log(e)
            var i = e.data.content
            wx.requestPayment({
              timeStamp: i.timeStamp,
              nonceStr: i.nonce_str,
              package: 'prepay_id=' + i.prepay_id,
              signType: 'MD5',
              paySign: i.paySign,
              success(res) {
                wx.request({
                  url: url + 'gateCash/payByWxSuccess',
                  data: {
                    "list": false,
                    "nodeId": "kdslfw",
                    "platform": 2,
                    "privId": "default",
                    "resId": "default",
                    "sendTime": time_ss,
                    "seq": 1,
                    "shopId": shopid,
                    "source": 11,
                    "sysVer": "1.0.0",
                    "token": "kdsjfoewi12ijsldks",
                    "type": 1,
                    "userId": userId,
                    "userType": 1,
                    "content": {
                      "tradeId": i.tradeId
                    },
                    "page": 1,
                    "limit": 15
                  },
                  method: "POST",
                  header: { "Content-Type": "application/JSON" },
                  success: function (e) {
                    // 更改状态
                    wx.request({
                      url: url + 'gateOrder/updateOrderStatus',
                      data: {
                        "list": false,
                        "nodeId": "kdslfw",
                        "platform": 2,
                        "privId": "default",
                        "resId": "default",
                        "sendTime": time_ss,
                        "seq": 1,
                        "shopId": shopid,
                        "source": 11,
                        "sysVer": "1.0.0",
                        "token": "kdsjfoewi12ijsldks",
                        "type": 1,
                        "userId": userId,
                        "userType": 1,
                        "content": {
                          "id": id,
                          "status": status
                        }
                      },
                      method: "POST",
                      header: { "Content-Type": "application/json" },
                      success: function (e) {
                        
                        wx.showToast({
                          title: '支付成功',
                          icon: 'success',
                          duration: 1000
                        })
                      }
                    })
                  }
                })
              },
              fail(res) {
                console.log(res)
              }
            })
          } else if (s == '0000'){   //余额支付  更改状态

            wx.request({
              url: url+'gateOrder/updateOrderStatus',
              data: {
                "list": false,
                "nodeId": "kdslfw",
                "platform": 2,
                "privId": "default",
                "resId": "default",
                "sendTime": time_ss,
                "seq": 1,
                "shopId": shopid,
                "source": 11,
                "sysVer": "1.0.0",
                "token": "kdsjfoewi12ijsldks",
                "type": 1,
                "userId": userId,
                "userType": 1,
                "content": {
                  "id": id,
                  "status": status
                }
              },
              method: "POST",
              header: { "Content-Type": "application/json" },
              success: function (e) {
                wx.showToast({
                  title: '支付成功',
                  icon: 'success',
                  duration: 1000
                });
                that.goto_payment();
                that.goto_request()
              }
            })
          }else{
            wx.showLoading({
              title: i_,
            });
            setTimeout(function () {
              wx.hideLoading()
            }, 1000)
          }

        }
      })
    } else {
      wx.request({
        url: url+'gateOrder/updateOrderStatus',
        data: {
          "list": false,
          "nodeId": "kdslfw",
          "platform": 2,
          "privId": "default",
          "resId": "default",
          "sendTime": time_ss,
          "seq": 1,
          "shopId": shopid,
          "source": 11,
          "sysVer": "1.0.0",
          "token": "kdsjfoewi12ijsldks",
          "type": 1,
          "userId": userId,
          "userType": 1,
          "content": {
            "id": id,
            "status": status
          }
        },
        method: "POST",
        header: { "Content-Type": "application/json" },
        success: function (e) {
          // wx.hideLoading()
          if (i == 3) {
            wx.showToast({
              title: '提现发货成功',
              icon: 'success',
              duration: 1000
            })
          } else {
            that.goto_request()
            wx.navigateTo({
              url: '../../Sign/Sign?id=' + id,
            })
          }
        }
      })
    }
  },
  // 删除订单
  shanchu_orders: function (e) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    var that = this
    var datas = this.data.order
    var id = e.currentTarget.id;
    console.log(id)

    wx.request({
      url: url+'gateOrder/delOrder',
      data: {
        "list": false,
        "nodeId": "kdslfw",
        "platform": 2,
        "privId": "default",
        "resId": "default",
        "sendTime": time_ss,
        "seq": 1,
        "shopId": shopid,
        "source": 11,
        "sysVer": "1.0.0",
        "token": "kdsjfoewi12ijsldks",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "id": id
        }
      },
      method: "POST",
      header: { "Content-Type": "application/json" },
      success: function (e) {
        console.log(e.data.result.code)
        var code = e.data.result.code
        var desc = e.data.result.desc
        let i = code.substring(4)
        console.log(i)
        if (i = '0000'){
          wx.showToast({
            title: desc,
            icon: 'loading',
            duration: 1000
          });
          wx.switchTab({
            url: '../../../../pages/Order/Order'
          })
        }
      }
    })
  },
  // 取消订单
  quxiao_orders: function (e) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    var that = this
    var datas = this.data.order
    var id = e.currentTarget.id;
    var idx = e.currentTarget.dataset.idx
    wx.request({
      url: url+'gateOrder/updateOrderStatus',
      data: {
        "list": false,
        "nodeId": "kdslfw",
        "platform": 2,
        "privId": "default",
        "resId": "default",
        "sendTime": time_ss,
        "seq": 1,
        "shopId":shopid,
        "source": 11,
        "sysVer": "1.0.0",
        "token": "kdsjfoewi12ijsldks",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "id": id,
          "status": "6"
        }
      },
      method: "POST",
      header: { "Content-Type": "application/json" },
      success: function (e) {
        console.log(e)
        var code = e.data.result.code
        var desc = e.data.result.desc
        let i = code.substring(4)
        console.log(i)
        if (i = '0000') {
          wx.showToast({
            title: desc,
            icon: 'loading',
            duration: 1000
          });
          wx.switchTab({
            url: '../../../../pages/Order/Order'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: url+'gateOrder/searchOrderById',
      data: {
        "list": false,
        "nodeId": "kdslfw",
        "platform": 2,
        "privId": "default",
        "resId": "default",
        "sendTime": time_ss,
        "seq": 1,
        "shopId": shopid,
        "source": 11,
        "sysVer": "1.0.0",
        "token": "kdsjfoewi12ijsldks",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "id": order_id
        }
      },
      method: "POST",
      header: { "Content-Type": "application/json" },
      success: function (e) {
        wx.hideLoading()
        var datas = e.data.content
        console.log(datas)
        that.setData({
          datas: datas
        })
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})